import { AlertController, ModalController } from '@ionic/angular';
import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { EventService } from 'src/app/shared/service/event.service';

@Component({
  selector: 'app-send-label',
  templateUrl: './send-label.component.html',
  styleUrls: ['./send-label.component.scss'],
})
export class SendLabeComponent implements OnInit {
  @Input() tagData;
  sendLabelValue: any;
  noShowFootBtn = true;
  timer: any;
  submit: any = {};
  constructor(
    private appGlobalService: AppGlobalService,
    public alertController: AlertController,
    private httpService: HttpService,
    public modalController: ModalController,
    private eventService: EventService,
  ) { }

  ngOnInit() {

  }

  ionChange(e) {
    e.stopPropagation();

    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.sendLabelValue = e.detail.value;
    }, 100);
  }

  async sendLabel(e) {
    e.stopPropagation();
    const submit = this.appGlobalService.sendLabelMsg;
    if (submit && submit.content.label) {
      if (this.sendLabelValue === '') {
        this.appGlobalService.showToast('评论内容不能为空!', 1500, 'middle');
        return;
      } else {
        const itemContent = JSON.parse(JSON.stringify(submit.content));
        itemContent[itemContent.label] = this.sendLabelValue;
        const params = {
          content: itemContent,
          id: submit.id,
          sessionId: localStorage.getItem("sessionId")
        };
        await this.httpService.post("application/submit", params, res => {
          if (res.success) {
            this.sendLabelValue = '';
            this.modalController.dismiss();
            this.eventService.eventEmit.emit('sendLabel', res.data.eventResult);
          }
          this.appGlobalService.showToast(res.message, 1500, 'middle');
        });
      }
    }
  }
}
